<?php

namespace app\home\controller;

use app\common\product\model\Product;
use app\common\product\model\StoreCategory;
use think\Request;
use util\Tree;

class Goods
{
    protected $request = null;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }


    public function getGoodsList(): \think\response\Json
    {
        $post = $this->request->post();

        if (empty($post['cate_id'])) {
            return error([], "参数错误");
        }

        $model = new Product();
        $list = $model->whereRaw('FIND_IN_SET(?, cate_id)', [$post['cate_id']])->order('sort desc,id')->select();
        $products = [];
        foreach ($list as $item) {
            $item->append(['spec_list', 'sku_list', 'price_range']);
            $products[] = [
                "saasId" => 10000,
                "storeId" => 10000,
                "spuId" => $item['id'],
                "title" => $item['title'],
                "primaryImage" => $item['image'], // 推荐图
                "thumb" => $item['image'], // 推荐图
                "images" => $item["slider_image"], // 图集
                "video" => '',
                "available" => $item['status'],
                "price" => $item['price_range']['price'],
                "originPrice" => $item['price_range']['otPrice'],
                "soldNum" => $item['sales'], // 销量
                "isPutOnSale" => $item['status'], // 是否上架
                "categoryIds" => $item['cate_id'], // 分类,数组id
                "specList" => $item['spec_list'], // spec 列表 参考attr
                "skuList" => $item['sku_list'], // sku 列表 参考attr
                "spuTagList" => [], // spuTag 标签
                "limitInfo" => $item['limit_info'], // limit 限制
                "desc" => $item['intro'],
                "etitle" => '',
            ];
        }

        $result['spuList'] = $products;
        $result['totalCount'] = $model->count();

        return success($result);
    }

    public function getGoodsDetail(): \think\response\Json
    {
        $post = $this->request->post();
        if (empty($post['spuId'])) {
            return error([], "参数错误");
        }
        $model = new Product();
        $row = $model->where('id', $post['spuId'])->find();
        if (empty($row)) {
            return error([], "商品不存在");
        }
        $row->append(['spec_list', 'sku_list', 'price_range', 'format_slider_image', 'desc']);
        $result['details'] = [
            "saasId" => 10000,
            "storeId" => 10000,
            "spuId" => $row['id'],
            "title" => $row['title'],
            "primaryImage" => $row['image'],
            "thumb" => $row['image'],
            "images" => $row["format_slider_image"],
            "video" => '',
            "price" => $row['price_range']['price'],
            "originPrice" => $row['price_range']['otPrice'],
            "soldNum" => $row['sales'],
            "isPutOnSale" => $row['is_show'],
            "categoryIds" => $row['cate_id'],
            "specList" => $row['spec_list'],
            "skuList" => $row['sku_list'],
            "spuTagList" => [],
            "limitInfo" => $row['limit_info'],
            "desc" => $row['intro'],
            "etitle" => '',
            'spuStockQuantity' => 100,
            "available" => 1,
            "minSalePrice" => $row['price_range']['minSalePrice'],
            "minLinePrice" => $row['price_range']['minLinePrice'],
            "maxSalePrice" => $row['price_range']['maxSalePrice'],
            "maxLinePrice" => $row['price_range']['maxLinePrice'],
            "desc" => $row['desc']
        ];

        return success($result);
    }

    public function getActivityList(): \think\response\Json
    {
        return success(['activityList'=>[]]);
    }

    public function getCategoryTree(): \think\response\Json
    {
        $model = new StoreCategory();
        $rows = $model->field('id,parent_id,title,big_pic')->order('sort,id')->select();

        if ($rows->isEmpty()) {
            return success([], "分类不存在");
        } else {
            $list = $rows->toArray();
            $result['tree'] = Tree::buildTree([], $list, 0);
        }

        return success($result);
    }
}